package com.qunar.wan.service;
import com.qunar.wan.common_api.model.Activity;
import com.qunar.wan.common_api.model.ActivityIndex;
import com.qunar.wan.common_api.service.ActivityService;
import com.qunar.wan.common_api.service.ConvertService;
import com.qunar.wan.common_api.service.UserService;
import com.qunar.wan.support.lucene.HighLighter;
import com.qunar.wan.util.DateUtil;
import com.qunar.wan.util.HTMLTagsUtil;
import org.apache.lucene.document.*;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * Created by hiCyoung on 13-12-1.
 */
@Service("activityConvertService")
public class ActivityConvertServiceImpl implements ConvertService<Activity, ActivityIndex> {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Resource
    private ActivityService activityService;
    @Resource
    private UserService userService;

    @Override
    public Document convert2Doc(Activity activity) {
        Document document = new Document();
        document.add(new StringField("id", String.valueOf(activity.getId()), Field.Store.YES));
        document.add(new TextField("title", activity.getTitle(), Field.Store.YES));
        // document.add(new Field("title", activity.getTitle(), TYPE_STORED_TERMVECTOR));
        /** 去掉HTML标签 */
        document.add(new TextField("summary", HTMLTagsUtil.delHTMLTag(activity.getDescription()), Field.Store.NO));
        document.add(new TextField("place", activity.getPlace(), Field.Store.YES));
        /** 用于时间范围搜索 */
        document.add(new LongField("start", activity.getStartTime().getTime(), Field.Store.YES));
        return document;
    }

    @Override
    public ActivityIndex convert2IndexObj(Document document, Query query) {
        checkNotNull(document);
        ActivityIndex activityIndex = new ActivityIndex();
        Activity activity = checkNotNull(activityService.selectById(Integer.parseInt(document.get("id"))));
        activityIndex.setId(Integer.parseInt(document.get("id")));
        try {
            activityIndex.setTitle(HighLighter.light(document.get("title"), query, "title"));
        } catch (Exception e) {
            logger.error("Activity title部分内容高亮异常{}", e);
        }
        try {
            activityIndex.setSummary(HighLighter.light(activity.getDescription(), query, "summary"));
        } catch (Exception e) {
            logger.error("Activity summary部分内容高亮异常{}", e);
        }
        try {
            activityIndex.setPlace(HighLighter.light(document.get("place"), query, "place"));
        } catch (Exception e) {
            logger.error("Activity place部分内容高亮异常{}", e);
        }
        activityIndex.setPoster(activity.getPoster());
        activityIndex.setAvgFee(activity.getAvgFee());
        activityIndex.setPeopleNum(activity.getPeopleNum());
        activityIndex.setSponsor(checkNotNull(userService.selectUserById(activity.getUserId()).getUserName()));
        activityIndex.setStartDay(DateUtil.getDay(activity.getStartTime()));
        activityIndex.setStartHHMM(DateUtil.getHHMM(activity.getStartTime()));
        return activityIndex;
    }

    @Override
    public Term updateIndexTerm(Activity activity) {
        return new Term("id", activity.getId() + "");
    }

    // public static final FieldType TYPE_STORED_TERMVECTOR = new FieldType();
    //
    // static {
    // TYPE_STORED_TERMVECTOR.setIndexed(true);
    // TYPE_STORED_TERMVECTOR.setTokenized(true);
    // TYPE_STORED_TERMVECTOR.setStored(true);
    // TYPE_STORED_TERMVECTOR.setStoreTermVectors(true);
    // TYPE_STORED_TERMVECTOR.setStoreTermVectorPositions(true);
    // TYPE_STORED_TERMVECTOR.freeze();
    // }
}
